package com.ldy.core.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.ldy.core.common.AuditLogInterceptor;
import com.ldy.core.common.LoginUserHandlerMethodArgumentResolver;
import com.ldy.core.common.MyAuthorizationInterceptor;

/**
 * <p>
 * 此配置类可配置拦截器、参数解析器、返回值解析器、跨域支持等等
 * </p>
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	@Autowired
	private MyAuthorizationInterceptor authorizationInterceptor;
	@Autowired
	private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;

	@Value("${app.exclude-path-patterns:-}")
	private List<String> excludePathPatterns;
	
	@Value("${app.audit-log:false}")
	private Boolean auditLog;

	@Autowired
	private AuditLogInterceptor auditLogInterceptor;

	/**
	 * 拦截器配置
	 * 
	 * @param registry
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		InterceptorRegistration patterns = registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**");
		if (auditLog) {
			// 审计日志
			registry.addInterceptor(auditLogInterceptor).addPathPatterns("/**");
		}
		patterns.excludePathPatterns("/**/order/callback").excludePathPatterns("/**/vip/selectList")
				.excludePathPatterns("/**/user/logout").excludePathPatterns("/**/datav/**")
				.excludePathPatterns("/**/file/selectList").excludePathPatterns("/**/file/getBase64")
				.excludePathPatterns("/**/file/download").excludePathPatterns("/**/file/upload")
				.excludePathPatterns("/**/article/selectPage").excludePathPatterns("/**/license/selectPage")
				.excludePathPatterns("/**/work/report/apple/selectPage").excludePathPatterns("/**/work/report/insert")
				.excludePathPatterns("/**/work/report/sendEcode").excludePathPatterns("/**/work/worker/selectList")
				.excludePathPatterns("/**/article/selectList").excludePathPatterns("/**/app/selectList")

				.excludePathPatterns("/**/user/autoLogin").excludePathPatterns("/**/user/phoneLogin")
				.excludePathPatterns("/**/user/sendEcode").excludePathPatterns("/**/user/scan/login")
				.excludePathPatterns("/**/chat/createSse").excludePathPatterns("/**/user/image")
				.excludePathPatterns("/**/magic/web/**").excludePathPatterns("/**/v2/api-docs/**")
				.excludePathPatterns("/**/swagger-ui.html").excludePathPatterns("/**/webjars/**")
				.excludePathPatterns("/**/swagger-resources/**").excludePathPatterns("/**/user/sendEcode")
				.excludePathPatterns("/**/user/uppwd").excludePathPatterns("/**/user/registerEcode")
				.excludePathPatterns("/**/setting/getConfig")
				// openapi
				.excludePathPatterns("/**/open-api/**")
				// 支付
				.excludePathPatterns("/**/pay/v3/**")
				// 小程序白名单接口
				.excludePathPatterns("/**/wechat/**").excludePathPatterns("/**/user/login");

		// 配置文件配置排除
		patterns.excludePathPatterns(excludePathPatterns);
	}

	/**
	 * 跨域支持配置
	 * 
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedOrigins("*").allowCredentials(true)
				.allowedMethods("GET", "PUT", "DELETE", "POST", "OPTIONS").maxAge(3600);
	}

	/**
	 * 参数解析配置
	 * 
	 * @param resolvers
	 */
	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
		resolvers.add(loginUserHandlerMethodArgumentResolver);
	}
}